﻿function DateDiff(sDate1, sDate2) {     //sDate1和sDate2是2004-10-18格式 
    var aDate, oDate1, oDate2, iDays
    aDate = sDate1.split("-")
    oDate1 = new Date(aDate[1]*1 + '- ' + aDate[2] + '- ' + aDate[0])     //转换为10-18-2004格式
    aDate = sDate2.split("-")
    oDate2 = new Date(aDate[1]*1 + '- ' + aDate[2] + '- ' + aDate[0])
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24)     //把相差的毫秒数转换为天数 
    return iDays
}

function DateFormat(str) {
    str = str.replace("年", "-").replace("月", "-").replace("日", "");
    //alert(str);
    Astr = str.split("-");
    return Astr[0] * 1 + '-' + Astr[1] * 1 + "-" + Astr[2] * 1;
}


function DateFormat2(str) {

    Astr = str.split("-");
//    if (Astr[1] < 10) {
//        newM = "0" + Astr[1];
//    }
    return Astr[0] * 1 + '年' + Astr[1] + "月" + Astr[2] * 1 + "日";
}

function DateAdd(str, n) {
    var dd, mm, yy;
    var reg = /^(\d{4})-(\d{1,2})-(\d{1,2})$/;
    if (arr = str.match(reg)) {
        yy = Number(arr[1]);
        mm = Number(arr[2]) - 1;
        dd = Number(arr[3]);
    } else {
        var d = new Date();
        yy = d.getUTCFullYear();
        mm = ("00" + (d.getUTCMonth())).slice(-2);
        dd = ("00" + d.getUTCDate()).slice(-2);
    }
    return date2str(yy, mm, dd, n);
}


function date2str(yy, mm, dd, n) {
    var s, d, t, t2;
    t = Date.UTC(yy, mm, dd);
    t2 = n * 1000 * 3600 * 24;
    t += t2;
    d = new Date(t);
    s = d.getUTCFullYear() + "-";
    s += ("00" + (d.getUTCMonth() + 1)).slice(-2) + "-";
    s += ("00" + d.getUTCDate()).slice(-2);
    return s;
}